package task.nms;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class BeamKnife extends NmsTask {
	private String schema;
	private String address;
	@Override
	public String getDescription() {
		String desc = "Beam Knife will totally remove Network elements from the DB\n" +
				" The 'address' parameter must be set to a comma separated list of ip addresses identifying the NEs.\n" +
				" example: 10.8.1.1,10.8.1.2\n" +
				" A remove script will be produced.";
		
		return desc;
	}
	@Override
	protected void doRun(Map<String, String> propMap) throws SQLException {
		schema = propMap.get("schema");
		address = propMap.get("address");
		address = adjust(address);
		String sql = String.format("select address, user_label from %s.MED_DISCOVERED_ELEMENTS de, " +
				"%s.STATE_COMMON_RESOURCE cr where de.NAME=cr.SD_NAME and address in (%s)", schema, schema, address);
		PreparedStatement stmt = conn().prepareStatement(sql);

		System.out.printf("-- This script will remove the NE s:%n");
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			System.out.printf("--  %s (%s)%n", rs.getString(1), rs.getString(2));
		}
		rs.close();
		stmt.close();

		System.out.println();
		String medDisElemRows = String.format("%s.MED_DISCOVERED_ELEMENTS where address in (%s)", schema, address);
		String neIdPks = String.format("select ID_PK from %s.STATE_MANAGED_ELEMENTS %n " +
				"where SD_NAME in %n" +
				"(select NAME from %s)", schema, medDisElemRows);
//		System.out.printf("neIdPks: %s%n", neIdPks);
		
		System.out.printf("%ndelete from %s.STATE_RESOURCES_IN_DOMAINS where COMMON_RESOURCE_FK in (%s);%n%n", schema, neIdPks);
		
		String commResr = String.format("from %s.STATE_COMMON_RESOURCE cr, %s.STATE_MANAGED_ELEMENTS me%n " +
				"where cr.SD_NAME like me.SD_NAME || '%%' and me.SD_NAME in %n" +
				"(select NAME from %s)", schema, schema, medDisElemRows);
		
		String idPks = String.format("select cr.ID_PK %s", commResr);
//		System.out.printf("idPks: %s%n", idPks);
		
		String[] tables = { "STATE_EQUIPMENT_HOLDERS", "STATE_EQUIPMENT" };
		for (String table : tables) {
			System.out.printf("delete from %s.%s where ID_PK in%n (%s);%n%n",schema, table, idPks);
		}

		System.out.printf("delete from %s.%s where COMMON_RESOURCE_FK in%n (%s);%n%n",schema, "STATE_COMMON_RESOURCE_REGISTRY", idPks);
		
		System.out.printf("delete from %s.STATE_COMMON_RESOURCE where ID_PK in (%s);%n%n ", schema, idPks);
		
		System.out.printf("delete from %s.STATE_MANAGED_ELEMENTS where SD_NAME in%n (select NAME from %s);%n%n", schema, medDisElemRows);
		 
		System.out.printf("delete from %s;%n", medDisElemRows);
	}
	
	private String adjust(String address) {
		String[] tab = address.split(",");
		StringBuilder sb = new StringBuilder();
		String sep = "";
		for (String s : tab) {
			sb.append(sep);
			if (s.charAt(0) != '\'') sb.append('\'');
			sb.append(s.trim());
			if (s.charAt(s.length()-1) != '\'')  sb.append('\'');
			sep = ",";
		}
		return sb.toString();
	}
	@Override
	public String[] propertyNames() {
		String[] props = { "schema", "address" };
		return props;
	}
}
